//
//  ViewController.m
//  TestAfn2
//
//  Created by xujingjing on 16/6/15.
//  Copyright © 2016年 xujingjing. All rights reserved.
//

#import "ViewController.h"
#import "NewsListModel.h"
#import "MJNewsListModel.h"
#import "HealthModel.h"
#import "MJhealthModel.h"
#import "TableViewDelegate.h"
#import "TableViewDataSource.h"
@interface ViewController ()
{
    TableViewDataSource *_tableViewDataSource;
    TableViewDelegate *_tableViewDelegate;
    
}
@property (strong,nonatomic)NewsListModel *newsModel;
@property (strong,nonatomic)HealthModel *helthModel;
@property (strong,nonatomic)MJhealthModel *mjHealthModel;
@property (strong,nonatomic)MJNewsListModel *mjnewsModel;


@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *textLable;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self testJsonModelApi];
    [self dealTableView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getNetworkState:) name:@"NETWORKSTATE" object:nil];
    
}
-(void)dealTableView{
    _tableViewDataSource=[[TableViewDataSource alloc] init];
    _tableViewDelegate=[[TableViewDelegate alloc] init];
    _tableView.dataSource=_tableViewDataSource;
    _tableView.delegate=_tableViewDelegate;

}
- (void)testJsonModelApi{
   
    [TestAfnetHttp postWithUrl:[NSString stringWithFormat:@"%@?num=%@",API_URL,@"10"] paramters:nil success:^(id response) {
        
        NSLog(@"api请求－>:%@",response);
        
     //   NSError *error;
     //   [self jsonModelTest:response error:error];  //jsonModel转模型方法
        
      [self mjModelTest:response];//mjExtention转模型
        
      [_tableView reloadData];
    } failed:^(id error) {
        
        NSLog(@"error->%@",error);
    }];

}
- (void)getNetworkState:(NSNotification *)notification{

    [_textLable setTitle:[NSString stringWithFormat:@"当前网络情况:%@",notification.userInfo[@"netState"]] forState:UIControlStateNormal];
    
}
- (void)jsonModelTest:(NSDictionary *)response error:(NSError *)error{
    
    _newsModel = [[NewsListModel alloc]initWithDictionary:response error:&error];
    
    //遍历获取小模型
    _helthModel = _newsModel.newslist[0];
    
//    NSLog(@"_newsModel:%@,code=%d",_newsModel.newslist,_newsModel.code);
//    NSLog(@"_helthModel.ctime=:%@,picUrl=%@,title=%@,url=%@",_helthModel.ctime,_helthModel.picUrl,_helthModel.title,_helthModel.url);

}
- (void)mjModelTest:(NSDictionary *)response{
   
    _mjnewsModel = [MJNewsListModel mj_objectWithKeyValues:response];
    
    _mjHealthModel = [MJhealthModel mj_objectWithKeyValues:_mjnewsModel.newslist[0]];
    _tableViewDataSource.mjnewsArray =_mjnewsModel.newslist;

//    NSLog(@"newsModel:%@,code=%d",newsModel.newslist,newsModel.code);
//    NSLog(@"mjHealthModel,picUrl=%@,title=%@,url=%@",_mjHealthModel.picUrl,_mjHealthModel.title,_mjHealthModel.url);
 
//归档
   // [self testMJEncodeDecoding];

}
- (void)testMJEncodeDecoding{
   
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path=[docPath stringByAppendingPathComponent:@"person.txt"];
    NSLog(@"path=%@",path);
    
    [MJhealthModel mj_ignoredCodingPropertyNames];
    NSString *file = [NSHomeDirectory() stringByAppendingPathComponent:@"person.txt"];
    // Encoding
    [NSKeyedArchiver archiveRootObject:_mjHealthModel toFile:file];
    
    // Decoding
    MJhealthModel *decodedBag = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
    NSLog(@"picUrl=%@, title=%@", decodedBag.picUrl, decodedBag.title);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
